// 引入vue-router模块
import {
    createRouter,
    createWebHashHistory
} from "vue-router"
// 使用index
import Index from '../src/views/Index.vue'
import store from "../store/index"

const router = new createRouter({
    history: createWebHashHistory(),
    routes: [
        //默认访问首页
        {
            path: "/",
            redirect: "/index"
        },
        //首页路由
        {
            path: "/index",
            name: "index",
            component: Index
        },
        //注册路由
        {
            path: '/register',
            name: 'register',
            component: () =>
                import('../src/views/Register.vue')
        },
        //登陆路由
        {
            path: "/login",
            name: "login",
            component: () =>
                import("../src/views/Login.vue"),
                beforeEnter:(to,from,next)=>{
                    if(localStorage.getItem("token")){
                        next(from.path)
                    }else{
                        next()
                    }
                }
        },
        //商品列表1路由
        {
            path: "/shoppinglist",
            name: "shoppinglist",
            component: () =>
                import("../src/views/ShoppingList.vue"),
            children: [{
                path: 'quicktour',
                component: import('../src/components/Quicktour.vue'),
            }, ],
        },
        //商品列表2路由
        {
            path: "/list",
            name: "list",
            component: () =>
                import("../src/views/List.vue"),
            children: [{
                path: 'quicktour',
                component: import('../src/components/Quicktour.vue'),
            }, ],
        },
        //购物车路由
        {
            path: "/cart",
            name: "cart",
            component: () =>
                import("../src/views/Cart.vue"),
            beforeEnter(to, form, next) {
                if (localStorage.getItem("token")) {
                    next()
                } else {
                    let isCat = confirm("登录之后，才能进入购物车，是否登录？")
                    if (isCat) {
                        router.push("/login")
                    } else {
                        return false
                    }
                }
            },
        },
        //详情路由
        {
            path: "/details",
            name: "details",
            component: () =>
                import("../src/views/Details.vue")
        },
        //用户路由
        {
            path: "/user",
            name: "user",
            component: () =>
                import("../src/views/User.vue"),
            children: [{
                    path: '',
                    component: () => import("../src/views/User_children.vue"),
                },
                {
                    path: 'useredit',
                    component: () => import("../src/views/UserEdit.vue"),
                },
                {
                    path: 'address_children',
                    component: () => import("../src/views/Address_children.vue"),
                },
                {
                    path: 'addressedit',
                    component: () => import("../src/views/AddressEdit.vue"),
                }
            ]
        },
        //搜索路由
        {
            path: "/search",
            name: "search",
            component: () =>
                import("../src/views/Search.vue"),
            children: [{
                path: 'quicktour',
                component: import('../src/components/Quicktour.vue'),
            }, ],
        },
    ]
})

//路由前置守卫
router.beforeEach((to, from) => {

})
//路由后置守卫
router.afterEach((to, from, next) => {
    window.scrollTo(0, 0);
});

// 暴露router
export default router